package com.travelsky.airchina.boarding.web.validator;

import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;

import com.travelsky.airchina.boarding.domain.User;
import com.travelsky.airchina.boarding.service.UserService;
/**
 * 系統管理員驗證
 * @author haichenwang
 *
 */
public class UserValidator {
	public void validate(User user,UserService userService,String confirmPassword, Errors errors) {
		String name = user.getName();
		if (!StringUtils.hasLength(name)) {
			errors.rejectValue("name", "required",new Object[]{"姓名"}, "required");
		} 
		if (!StringUtils.hasLength(user.getLoginName())) {
			errors.rejectValue("loginName", "required",new Object[]{"登录帐号"}, "required");
		} else if (user.getId()==null && userService.validateUserLoginNameExist(user.getLoginName())) {
			errors.rejectValue("loginName", "duplicate", new Object[]{user.getLoginName()},"already exists");
		}
		if (!StringUtils.hasLength(user.getLoginPassword())) {
			errors.rejectValue("loginPassword", "required", new Object[]{"登录密码"},"required");
		}else{
			if (!user.getLoginPassword().equals(confirmPassword)) {
				errors.rejectValue("loginPassword", "notsame", new Object[]{"登录密码和确认密码"},"不一样");
			}
		}
	}
}
